import type { Meta, StoryObj } from "@storybook/react-vite";
import { mockIPC } from "@tauri-apps/api/mocks";
import { Chat, ChatProps } from "./Chat";
import { Message } from "../model/bindings/Message";
import moment from "moment";

const meta = {
  component: Chat,
} satisfies Meta<typeof Chat>;

export default meta;

type Story = StoryObj<typeof meta>;

const date = new Date("2025-08-01 15:00:00").toISOString();
// note normalExpiry here is based on 'now' because Chat's internals compare with 'now' (in reality normalExpiry is 14 days after receivedAt/sentAt)
const normalExpiry = moment().add(14, "days").toISOString();
const customExpiry = moment().add(30, "days").toISOString();
const nearExpiry = moment().add(2, "days").toISOString();
const urgentExpiry = moment().add(12, "hours").toISOString();
const userPk =
  "700b6e6f19fcf2d88103baf44d9ed097dae3f8c372825d85b74b95c1a90c6b6e";

// Mocks the Rust IPC call that checks the length of the message.
// This number is used to fill up the bar under the chat input text field.
const beforeEachMockIPC = () =>
  mockIPC((cmd) => (cmd === "check_message_length" ? 0.5 : undefined));

const u2jMessage = {
  type: "userToJournalistMessage",
  id: BigInt(10),
  userPk,
  message: "Hey there fella",
  receivedAt: date,
  normalExpiry: normalExpiry,
  customExpiry: null,
  read: true,
} satisfies Partial<Message>;

const j2uMessage = {
  type: "journalistToUserMessage",
  id: BigInt(10),
  userPk,
  message: "Hey there fella",
  normalExpiry: normalExpiry,
  customExpiry: null,
  isSent: true,
  sentAt: date,
} satisfies Partial<Message>;

const mockU2JMessage = (
  id: number,
  message: string,
  normalExpiryOverride?: string,
) => ({
  ...u2jMessage,
  id: BigInt(id),
  normalExpiry: normalExpiryOverride ?? normalExpiry,
  message,
});
const mockJ2UMessage = (
  id: number,
  message: string,
  normalExpiryOverride?: string,
) => ({
  ...j2uMessage,
  id: BigInt(id),
  normalExpiry: normalExpiryOverride ?? normalExpiry,
  message,
});

const commonArgs = {
  userAutogeneratedName: "bountiful harvest",
  userReplyKey: userPk,
  currentUserStatus: "ACTIVE",
  userAlias: "Potentially interesting source",
  userDescription: "Responded from the callout",
  markAsUnread: () => {},
  showEditModal: () => {},
  showMuteModal: () => {},
  showCopyToClipboardModal: () => {},
} satisfies Partial<ChatProps>;

export const Default: Story = {
  args: {
    ...commonArgs,
    messages: [
      mockU2JMessage(1, "Hi, I'm a very important source."),
      mockJ2UMessage(2, "Have you got anything interesting to say?"),
      mockU2JMessage(3, "No, not really."),
      mockJ2UMessage(4, "I see. Please come back when you do."),
    ],
  },
  beforeEach: beforeEachMockIPC,
};

export const NearExpiry: Story = {
  args: {
    ...commonArgs,
    messages: [
      mockU2JMessage(1, "Hi, I'm a very important source.", nearExpiry),
      mockJ2UMessage(
        2,
        "Have you got anything interesting to say?",
        nearExpiry,
      ),
    ],
  },
  beforeEach: beforeEachMockIPC,
};

export const UrgentExpiry: Story = {
  args: {
    ...commonArgs,
    messages: [
      mockU2JMessage(1, "Hi, I'm a very important source.", urgentExpiry),
      mockJ2UMessage(
        2,
        "Have you got anything interesting to say?",
        urgentExpiry,
      ),
    ],
  },
  beforeEach: beforeEachMockIPC,
};

export const CustomExpiry: Story = {
  args: {
    ...commonArgs,
    messages: [
      {
        ...mockU2JMessage(1, "Hi, I'm a very important source."),
        customExpiry,
      },
      {
        ...mockJ2UMessage(2, "Have you got anything interesting to say?"),
        customExpiry,
      },
    ],
  },
  beforeEach: beforeEachMockIPC,
};
